﻿<?php
    
    if(isset($_REQUEST['id']) == true)
    {
        include_once("code/DataProvider.php");
        $masp = $_REQUEST['id'];
        $query = "SELECT * FROM sanpham WHERE MaSP = '".$masp."'";
        $result = DataProvider::executeQuery($query);
        
        
        
        
        
        while($row = mysql_fetch_array($result))
        {
            $query1 = " SELECT ct.MaSP FROM sanpham sp, chitietddh ct WHERE ct.MaSP = sp.MaSP ";
            $result1 = DataProvider::ExecuteQuery($query1);
            $row1 = mysql_fetch_array($result1);
            if(($row['MaSP'] == $row1['MaSP']))
            {
                if($row['BiXoa'] == 1)
                {
                    echo '<script type="text/javascript">';
                    echo 'alert("Bỏ xóa thành công!")';
                    echo '</script>';
                    $query = " UPDATE sanpham SET BiXoa = '0' WHERE MaSP = '".$masp."' ";
                    $result = DataProvider::executeQuery($query);
                }
                else
                {
                    echo '<script type="text/javascript">';
                    echo 'alert("Sản phẩm đã có người đặt, không thể xóa! ")';
                    echo '</script>';
                    $query = " UPDATE sanpham SET BiXoa = '1' WHERE MaSP = '".$masp."' ";
                    $result = DataProvider::executeQuery($query);
                }
            }
    
            if(!($row['MaSP'] == $row1['MaSP']))
            {
                if($row['BiXoa'] == 1)
                {
                    echo '<script type="text/javascript">';
                    echo 'alert("Bỏ xóa thành công!")';
                    echo '</script>';
                    $query = " UPDATE sanpham SET BiXoa = '0' WHERE MaSP = '".$masp."' ";
                    $result = DataProvider::executeQuery($query);
                }
                else
                {
                    echo '<script type="text/javascript">';
                    echo 'alert("Xóa thành công")';
                    echo '</script>';
                    $query = " DELETE FROM sanpham WHERE MaSP = '".$masp."' ";
                    $result = DataProvider::executeQuery($query);
                }
            }
        }
    }

        echo '<script type="text/javascript">';
        echo 'location="admin.php?act=9"';
        echo '</script>';
    
?>